<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="utf-8">
	<title>Admin Map My Feelings - Projet IFT604</title>
	<meta name="description" content="">
	<meta name="author" content="">

	<!-- Le HTML5 shim, for IE6-8 support of HTML elements -->
	<!--[if lt IE 9]>
	<script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
	<![endif]-->

	<!-- Le styles -->
	<link href="<?php echo base_url('assets/css/bootstrap.css') ?>" rel="stylesheet">
	<style type="text/css">
	body {
	padding-top: 60px;
	}

	h1{
		border-bottom: 1px solid #eee;
		margin-bottom: 20px;
		padding-bottom: 10px;
	}

	caption{
		font-weight: bold;
		font-size: 20px;
		padding: 10px;
	}

	.span16{
		padding-bottom: 20px;
	}

	section{
		padding-top: 60px;
	}
	</style>

	<!-- Le fav and touch icons -->
	<link rel="shortcut icon" href="images/favicon.ico">
	<link rel="apple-touch-icon" href="images/apple-touch-icon.png">
	<link rel="apple-touch-icon" sizes="72x72" href="images/apple-touch-icon-72x72.png">
	<link rel="apple-touch-icon" sizes="114x114" href="images/apple-touch-icon-114x114.png">
</head>

<body onload="visualiserClicked()">

	<div class="topbar">
		<div class="fill">
			<div class="container">
				<a class="brand" href="<?php echo base_url(); ?>">Admin MMF</a>
				<ul class="nav">
					<li><a href="#visualiser">Visualiser</a></li>
					<li><a href="#inserer">Inserer</a></li>
					<li><a href="#effacer">Effacer</a></li>
				</ul>
			</div>
		</div>
	</div>

	<div class="container">

		<section id="visualiser" style="padding: 0;">
			<div class="row">
				<div class="span16">
					<h1>Visualiser les feelings</h1>
					<button class="btn success" onclick="visualiserClicked();">Mettre a jour</button><br /><br />
					<div id="feelings"></div>
				</div>
			</div>
		</section>
		<section id="inserer">
		<div class="row">
			<div class="span16">
				<h1>Insérer des feelings</h1>
				<div id="generatedfeelings"></div>
				<button class="btn primary" onclick="valeursAleatoiresClicked();">Ajouter</button>
			</div>
		</div>
		</section>
		<section id="effacer">
		<div class="row">
			<div class="span16">
			<h1>Effacer les feelings</h1>
			<button class="btn success" onclick="effacerClicked();">Vider la table</button>
			</div>
		</div>
		</section>

	</div> <!-- /container -->
<script type="text/javascript">

function effacerClicked() {
	var x = confirm("Etes-vous sur de vouloir effacer toute la BD ?");
	if (x) {
		callback = function() { 
		   if(xhr_object.readyState == 4) {
			   alert(xhr_object.responseText); 
			   visualiserClicked();
		   }
		}
		
		requeteAJAX("empty_db", callback);
	}
}

function valeursAleatoiresClicked() {
	var x = prompt("Combien de valeurs voulez-vous ajouter ?");
	if (x) {
		requeteAJAX("new_rand_values/"+x, function() { 
		   if(xhr_object.readyState == 4) {
			elem = document.getElementById('generatedfeelings');
		   	elem.innerHTML = xhr_object.responseText;
		   	visualiserClicked();
		   }
		});
	}
}

function visualiserClicked() {
	requeteAJAX('get_feelings', function() { 
		   if(xhr_object.readyState == 4) {
			elem = document.getElementById('feelings');
		   	elem.innerHTML = xhr_object.responseText;
		   }
		});
}


function requeteAJAX(nameFonction, callback) {
	xhr_object = null; 
	 
	if(window.XMLHttpRequest) // Firefox 
	   xhr_object = new XMLHttpRequest(); 

	else if(window.ActiveXObject) // Internet Explorer 
	   xhr_object = new ActiveXObject("Microsoft.XMLHTTP"); 

	else { // XMLHttpRequest non supporté par le navigateur 
	       alert("Votre navigateur ne supporte pas les objets XMLHTTPRequest..."); 
	       return; 
	} 

	//xhr_object.open("GET", "https://pages.usherbrooke.ca/agervaise/checkin.php?fonction="+numFonction, true); 
	xhr_object.open("GET", "http://ift604.alixg.com/admin/"+nameFonction, true); 
	 
	xhr_object.onreadystatechange = callback;
	xhr_object.send(null);

}
</script>
</body>
</html>
